package com.miracle.moa.manager.controller;


import com.miracle.common.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.miracle.common.Result;
import com.miracle.moa.manager.entity.Role;
import com.miracle.moa.manager.service.IRoleService;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author miracle
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/manager/role")
public class RoleController {


    @Autowired
    private IRoleService roleService;

    /**
     * 查询角色列表
     *
     * @param page  当前页码
     * @param pageSize 每页展示条数
     * @param keyword 角色名称/编码
     * @return
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @GetMapping("/list")
    public IPage<Role> listAllRole(@RequestParam(name = "page") int page,
                                   @RequestParam(name = "pageSize") int pageSize,
                                   @RequestParam(name = "keyword") String keyword) {

        return roleService.rolePage(page, pageSize, keyword);
    }

    /**
     * 查询所有角色
     * @return
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @GetMapping("/listAll")
    public Result<List<Role>> listAll() {
        List<Role> roleList = roleService.list();
        return new Result<>(StatusCode.OK, "查询成功", roleList);
    }

    /**
     * 保存角色数据
     * @param role 角色信息
     */
    @PostMapping("/save")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result<Role> save(@Validated @RequestBody Role role) {
        return roleService.saveRole(role);
    }

    /**
     * 更新角色信息
     * @param role
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize(("hasAnyAuthority('admin')"))
    public Result<Role> update(@Validated @RequestBody Role role) {
        return roleService.updateRole(role);
    }

    /**
     * 删除角色信息
     * @param id 角色编码
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize(("hasAnyAuthority('admin')"))
    public Result delete(@PathVariable Integer id) {
        return roleService.deleteRole(id);
    }
}
